/*===============================================
*   文件名称：tcp_client.c
*   创 建 者: Chenqw
*   创建日期：2023年07月01日
*   描    述：
================================================*/
#include <stdio.h>
#include <string.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>
 #include <unistd.h>



int main(int argc, char *argv[])
{ 
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if(-1 == sockfd)
    {
        perror("socket");
        return -1;
    }

    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(6666);
    addr.sin_addr.s_addr = inet_addr("0");

    if(-1 == connect(sockfd, (struct sockaddr*)&addr, sizeof(addr)))
    {
        perror("connetc");
        return -1;
    }

    while(1)
    {
        char name[100];
        char passwd[100];
        char buf[20];
        int n;
        printf("1.登录 2.注册用户 3.更新用户信息 4.删除账户 5.退出系统");
        scanf("%d", &n);
        getchar();
        if(n == 1)
        {
            strcpy(buf,"login");
            write(sockfd, buf, sizeof(buf));
            
            printf("输入姓名：");
            gets(name);
            write(sockfd, name, sizeof(name));
            
            printf("输入密码：");
            gets(passwd);
            write(sockfd, passwd, sizeof(passwd));

            read(sockfd,buf, sizeof(buf));
            printf("结果是：%s\n", buf);
        }
        else if(n == 2)
        {
            strcpy(buf,"register");
            write(sockfd, buf, sizeof(buf));
            
            printf("输入姓名：");
            gets(name);
            write(sockfd, name, sizeof(name));
            
            printf("输入密码：");
            gets(passwd);
            write(sockfd, passwd, sizeof(passwd));

            read(sockfd,buf, sizeof(buf));
            printf("结果是：%s\n", buf);
        }
        else if(n == 3)
        {
            strcpy(buf,"update");
            write(sockfd, buf, sizeof(buf));
            
            printf("输入姓名：");
            gets(name);
            write(sockfd, name, sizeof(name));
            
            printf("输入密码：");
            gets(passwd);
            write(sockfd, passwd, sizeof(passwd));
            
            printf("输入之前的名字");
            char oldname[100];
            gets(oldname);
            write(sockfd, oldname, sizeof(oldname));

            read(sockfd,buf, sizeof(buf));
            printf("结果是：%s\n", buf);
        }
        else if(n == 4)
        {
            strcpy(buf,"delete");
            write(sockfd, buf, sizeof(buf));
            
            printf("输入名字：");
            gets(name);
            write(sockfd, name, sizeof(name));
            
            printf("输入密码：");
            gets(passwd);
            write(sockfd, passwd, sizeof(passwd));

            read(sockfd,buf, sizeof(buf));
            printf("结果是： %s\n", buf);
        }
        else if(n == 5)
        {
            close(sockfd);
        }
    }

    return 0;
} 
